#pragma once
#ifndef _THREADPOOL_H
#include <thread>
#include <vector>
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>
using namespace std;
struct TaskQueue
{
	TaskQueue(int capacity):_capacity(capacity){}
	queue<int> taskQue;
	bool _is_full()
	{
		return taskQue.size() >= _capacity;
	}
	bool _is_empty()
	{
		return taskQue.empty();
	}

	void push(int t)
	{
		if(taskQue.size() < _capacity)
			taskQue.push(t);
	}
	int get() 
	{
		if (taskQue.empty())return -1;
		int ret = taskQue.front();
		taskQue.pop();
		return ret;
	}
	int _capacity;
};
class ThreadPool
{
public:
	ThreadPool(int capacity):_capacity(capacity){}
	void start(function<void()> f)
	{
		_is_runing = true;
		callback = f;
		for (int i = 0; i < _capacity; i++)
		{
			threadList.emplace_back(thread(bind(&ThreadPool::circle, this)));
		}
	}
	void circle()
	{
		callback();
	}
	void setQue(TaskQueue* q)
	{
		_taskQue = q;
	}
	void stop()
	{
		_is_runing = false;
		for (auto& it : threadList)
		{
			it.join();
		}
	}
	
	bool _is_runing = false;
	mutex loc;
	condition_variable R_ONLY;
	condition_variable W_ONLY;

private:
	int _capacity;
	function<void()> callback;
	vector<thread> threadList;
	TaskQueue* _taskQue;
	

};
#endif // !_THREADPOOL_H
